﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;

namespace RealmSearcher
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		XmlDocument _RealmList;


		public const string CONST_TITLE_REGEX = "<title>.*?</title>";
		public const string CONST_DESC_REGEX = "<description>.*?</description>";
		public const string CONST_RSSURL = "http://www.worldofwarcraft.com/realmstatus/index.xml";

		private string[] GetRealmInfoFromOuterXML(string xmlString)
		{
			string status = String.Empty;
			string serverType = String.Empty;
			string name = string.Empty;
			string desc = string.Empty;

			MatchCollection serverInfo = Regex.Matches(xmlString, CONST_TITLE_REGEX, RegexOptions.IgnoreCase);
			foreach (Match m in serverInfo)
			{
				string fixedString = m.Value.Replace("<title>", String.Empty).Replace("</title>", String.Empty);
				int typeIndex = fixedString.IndexOf(Char.Parse("("));
				int typeEndIndex = fixedString.IndexOf(Char.Parse(")"));

				name = fixedString.Remove(typeIndex, fixedString.Length - typeIndex);

				string [] arrayName = fixedString.Split(Char.Parse(" "));
				status = arrayName[arrayName.Length - 1];

				serverType = fixedString.Remove(0, typeIndex).Replace(status, String.Empty).Replace("Realm", String.Empty);
					
			}

			if (status.Trim() == "Up")
			{
				MatchCollection descriptions = Regex.Matches(xmlString, CONST_DESC_REGEX, RegexOptions.IgnoreCase);
				foreach (Match m in descriptions)
				{
					string fixedDescString = m.Value.Replace("<description>", String.Empty).Replace("</description>", String.Empty);
					desc = fixedDescString.Replace("Population: ", String.Empty).Trim();
				}
			}

			String concatString = String.Format("{0};{1};{2};{3}", name.Trim(), serverType.Trim(), status.Trim(), desc.Trim());
			String[] realmInfo = concatString.Split(Char.Parse(";"));
			return realmInfo;
		}

		private void PopulatRealms()
		{
			_RealmList = new XmlDocument();
			_RealmList.Load(CONST_RSSURL);

            XPathNavigator nav = _RealmList.CreateNavigator();
			XPathExpression expr;
			expr = nav.Compile("/rss/channel/item");
			XPathNodeIterator iterator = nav.Select(expr);

			lstRealms.Items.Clear();

			while (iterator.MoveNext())
			{
				XPathNavigator realmData = iterator.Current.Clone();
				String[] realmInfo = GetRealmInfoFromOuterXML(realmData.OuterXml);

				ListViewItem item = new ListViewItem(realmInfo[0].Trim());
				item.SubItems.Add(realmInfo[1].Trim());
				item.SubItems.Add(realmInfo[2].Trim());
				item.SubItems.Add(realmInfo[3].Trim());
				lstRealms.Items.Add(item);
			}
		}

		private void getRealmStatusListToolStripMenuItem_Click(object sender, EventArgs e)
		{
			PopulatRealms();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}
	}
}
